#!/bin/bash
#######################################################################
# File Name: auto_install_zookeeper.sh
# Version: V1.0
# Author: Brian Hsiung
# Created Time : 2021-05-15 23:34:04
# Description: Zookeeper 集群部署脚本
# 1. 默认安装目录为/opt/zookeeper/zookeeper
# 2. 默认数据目录为/opt/zookeeper/data
# 3. 默认日志目录为/opt/zookeeper/log
# 4. zoo.cfg文件中的相关IP需要修改
# 5. 在zkEnv.sh文件中的ZOOKEEPER_PREFIX后面添加JAVA_HOME
######################################################################

. /etc/profile

DIR=$(cd $(dirname $0) && pwd)
cd $DIR

MY_ID=
JAVA_HOME=$(echo $JAVA_HOME)
ZOOKEEPER_PORT=2181
ZOOKEEPER_DATA=$DIR/data
ZOOKEEPER_LOG=$DIR/log
ZOOKEEPER_HOME=$DIR/zookeeper

# 当前主机IP
HOST_IP=$(hostname -I | awk '{print $1}')
# 此处的3个IP需要根据实际情况修改，三台服务器上的IP顺序不能变
ZK_IP_LIST=(192.168.157.131 192.168.157.132 192.168.157.133)

function init_env {
    if systemctl status firewalld &> /dev/null; then
        firewall-cmd --zone=public --add-port={$ZOOKEEPER_PORT/tcp,20881/tcp,30881/tcp} --permanent &>/dev/null
        firewall-cmd --reload &> /dev/null
    fi

    if echo "${ZK_IP_LIST[@]}" | grep -w "$HOST_IP" &> /dev/null; then
        echo "当前主机IP为: $HOST_IP"
    else
        echo "获取的主机IP: $HOST_IP 不在 ${ZK_IP_LIST[@]} 中, 请手动设置 HOST_IP"
        exit 1
    fi

    # 设置myid
    for ((i=0; i < ${#ZK_IP_LIST[@]}; i++)); do
        if [ "$HOST_IP" == "${ZK_IP_LIST[i]}" ]; then
            let i+=1
            MY_ID=$i
            echo "当前zookeeper的myid为: $MY_ID"
            break
        fi
    done

    if ! java -version &>/dev/null; then
        echo "当前服务器未安装jdk, 需要先安装1.8.0以上的jdk"
        # 解压后的内容是 jdk1.8.0_261
        tar -zxf server-jre-8u261-linux-x64.tar.gz -C /usr/local
        JAVA_HOME=/usr/local/jdk1.8.0_261
        ln -s $JAVA_HOME/bin/java /usr/bin/java
        echo "安装的JAVA_HOME为: $JAVA_HOME"
        if ! grep JAVA_HOME /etc/profile &> /dev/null; then
            echo "export JAVA_HOME=$JAVA_HOME" >> /etc/profile
            echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
        fi
    fi
}


function install_zookeeper {
    [ ! -d $ZOOKEEPER_DATA ] && mkdir -p $ZOOKEEPER_DATA
    [ ! -d $ZOOKEEPER_LOG ]  && mkdir -p $ZOOKEEPER_LOG
    
    tar -zxf apache-zookeeper*.tar.gz
    ln -s apache-zookeeper-*-bin $ZOOKEEPER_HOME
    echo "export PATH=\$PATH:$ZOOKEEPER_HOME/bin" > /etc/profile.d/zookeeper.sh

    echo $MY_ID > $ZOOKEEPER_DATA/myid
    # 建议加上这部分，如果没有/usr/bin/java, /etc/profile中也没有JAVA_HOME，则可能在启动的时候报找不到JAVA_HOME
    if [ -n "$JAVA_HOME" ]; then
        sed -ir "/^ZOOKEEPER_PREFIX/a JAVA_HOME="$JAVA_HOME"" $ZOOKEEPER_HOME/bin/zkEnv.sh
    fi
    cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg  $ZOOKEEPER_HOME/conf/zoo.cfg

cat > $ZOOKEEPER_HOME/conf/zoo.cfg << EOF
tickTime=2000
initLimit=10
syncLimit=5
clientPort=$ZOOKEEPER_PORT
dataDir=$ZOOKEEPER_DATA
dataLogDir=$ZOOKEEPER_LOG
server.1=${ZK_IP_LIST[0]}:20881:30881
server.2=${ZK_IP_LIST[1]}:20881:30881
server.3=${ZK_IP_LIST[2]}:20881:30881
maxClientCnxns=0
EOF

cat > /etc/rc.d/init.d/zookeeper << EOF
#!/bin/bash
#chkconfig:2345 20 80
#description:Zookeeper
#processname:Zookeeper

ZK_HOME=$ZOOKEEPER_HOME
case \$1 in
    start)
        \$ZK_HOME/bin/zkServer.sh start
        ;;
    stop)
        \$ZK_HOME/bin/zkServer.sh stop
        ;;
    status)
        \$ZK_HOME/bin/zkServer.sh status
        ;;
    restart)
        \$ZK_HOME/bin/zkServer.sh restart
        ;;
    *)
        echo "require start|stop|status|restart"
        ;;
esac
EOF

    chmod +x /etc/rc.d/init.d/zookeeper
    chkconfig --add zookeeper
    chkconfig zookeeper on
    service zookeeper start
}

init_env
install_zookeeper